/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.CT_TuyenTau_DTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Phung
 */
public class CT_TuyenTau_DAO {
    
    public static String Get_Ma_CT_TT() throws SQLException{
        // Chua Co Trong Store.
        String Ma_CT_TT = null;
        MySQLDataHelper_DAO.open();
        Connection Con = MySQLDataHelper_DAO.getConnection();
        CallableStatement statement = Con.prepareCall("{call Add_CT_ID(?)}");
        statement.setString(1, "CT_TT");
        statement.execute();
        ResultSet Rs = statement.getResultSet();
        if(Rs.next()){
            Ma_CT_TT = Rs.getString(1);
        }
        return Ma_CT_TT;
    }
    
    public static ArrayList<CT_TuyenTau_DTO> GetALL()
    {
        ArrayList<CT_TuyenTau_DTO> ds = new ArrayList<>();
        try {
            String sql = "Select * from CT_TuyenTau" ;
            MySQLDataHelper_DAO.open();
            ResultSet rs =  MySQLDataHelper_DAO.executeQuery(sql);
            while (rs.next()) {
                CT_TuyenTau_DTO CT_Tau = new CT_TuyenTau_DTO();
                CT_Tau.setID(rs.getString("ID"));
                CT_Tau.setMaGaDi(rs.getString("MaGaDi"));
                CT_Tau.setMaTT(rs.getString("MaTT"));
                CT_Tau.setThoiGianden(rs.getString("ThoiGianden"));
                CT_Tau.setViTriGaDen(rs.getString("ViTriGaDen"));
                CT_Tau.setMaDT(rs.getString("MaDT"));
                ds.add(CT_Tau);
            }
            MySQLDataHelper_DAO.close();
       }catch (SQLException ex) {
            System.out.println(ex);
        }
        return ds;
    }
    
    public static boolean Insert(CT_TuyenTau_DTO CT_Tau)
    {
        boolean flag = false;
        try {
            String sql = "Insert CT_TuyenTau Values('"+CT_Tau.getID()+"','"+CT_Tau.getMaGaDi()+"','"+CT_Tau.getMaTT()+"','"+CT_Tau.getThoiGianden()+"','"+CT_Tau.getViTriGaDen()+"','"+CT_Tau.getMaDT()+"')";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Update(CT_TuyenTau_DTO CT_Tau)
    {
        boolean flag = false;
        try {
            String sql = "Update CT_TuyenTau Set MaGaDi = '"+ CT_Tau.getMaGaDi() +"', MaTT = '"+CT_Tau.getMaTT()+"', ThoiGianDen = '"+CT_Tau.getThoiGianden()+"', ViTriGaDen = '"+ CT_Tau.getViTriGaDen()+ "', MaDT='"+CT_Tau.getMaDT()+"' where ID = '"+CT_Tau.getID()+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
    public static boolean Delete(String maID)
    {
        boolean flag = false;
        try {
            String sql = "Delete from CT_TuyenTau where ID = '"+maID+"'";
            MySQLDataHelper_DAO.open();
            int kq = MySQLDataHelper_DAO.executeUpdate(sql);
            if(kq == 1)
                flag =true;
            MySQLDataHelper_DAO.close();
        }catch (Exception ex) {
            System.out.println(ex);
        }
        return flag;
    }
    
}
